--[[
    NuckPluck - An android game for learning the Thai alphabet
    Copyright (C) 2013 Nathan Kieffer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Contact: nathankieffer@gmail.com
--]]

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local physics = require("physics")
local letter = require("letter")
local block = require("block")
physics.start()
local Map = {}

function Map.rc()
   return math.random(255), math.random(255), math.random(255)
end

function Map.newCell(data)
   local cell = display.newGroup()
   cell.x = data.x * data.d
   cell.y = data.y * data.d
   cell.name = string.format("Cell (%d, %d)",data.x,data.y)
   cell.type = "cell"
   local box
   cell.subtype = data.type
   cell.name = string.format("Cell (%d, %d)",data.x,data.y)
   local isSensor = nil
   local bodyType = nil
   if cell.subtype == "open" then
      box = display.newImage(cell, "images/tile_glass.png")
      isSensor = true
      bodyType = "dynamic"
      filter = { categoryBits = 2, maskBits = 0 }								
   elseif cell.subtype == "air" then
      box  = display.newRoundedRect(cell, 0,0, data.d - 2, data.d - 2, 10)
      box:setFillColor(0,0)
      isSensor = true
      cell.width = cell.width * 0.5
      cell.height = cell.height * 0.5
      box.width = box.width *0.5
      box.height = box.height * 0.5
      bodyType = "static"
      filter = { categoryBits = 32, maskBits = 17 }								
   elseif cell.subtype == "wall" then
      isSensor = true
      bodyType = "static"
      filter = {categoryBits=0x00010,  maskBits=0x00000 }
   
   elseif cell.subtype == "lava" then
      print("------------------here")
      local sheet = graphics.newImageSheet("images/tile_lava.png", {width=50, height=50, numFrames=4})
      box  = display.newSprite(cell, sheet, {name="roil", start=1, count=4, time=900, loopCount=0})--0,0, data.d - 2, data.d - 2, 10)
      box:play()
      isSensor = true
      bodyType = "static"
      filter = {categoryBits=32,  maskBits=1 }
   end
   cell:toFront()
   
   physics.addBody(cell, bodyType, {isSensor = isSensor, friction=friction, 
				   density=density, 
				   filter=filter})
   return cell
end

function Map.newMap(mapData, cellW, cellH, cellD)
   local map = display.newGroup()
   map.correct = 0
   map.name = "Map"
   map.cellW = cellW
   map.cellH = cellH
   map.cellD = cellD
   map.x = 0
   map.y = 0
   map.anchorX = 0.5
   map.anchorY = 0.5
   map.mapData = mapData
   function map:generate()
      self:clear()
      if self.mapData then
	 local y = 0
	 local lines = self.mapData.map:gmatch("[^\r\n]+")
	 for line in lines do
	    local x = 0
	    for i=1,string.len(line) do
	       local char = string.sub(line,i,i)
	       
	       if char == " " then
		  local cell = Map.newCell{x=x, y=y, d=self.cellD, type="open"}
		  self:insert(cell)
		  cell:toBack()
	       elseif char == "." then
		  local cell = Map.newCell{x=x, y=y, d=self.cellD, type="air"}
		  self:insert(cell)
		  cell:toBack()
	       elseif char == "^" then
		  local cell = Map.newCell{x=x, y=y, d=self.cellD, type="lava"}
		  self:insert(cell)
		  cell:toBack()
	       elseif char == "#" then
		  
		  local cell = Map.newCell{x=x, y=y, d=self.cellD, type="wall"}
		  self:insert(cell)
		  cell:toBack()
		  block.newBlock(self, x, y)
	       else
		  local cell = Map.newCell{x=x, y=y, d=self.cellD, type="open"}
		  self:insert(cell)
		  cell:toBack()
		  local char = mapData.data[tonumber(char)].char
		  local l = letter.newLetter(self, char, x, y)
	       end
	       x = x + 1
	    end
	    y = y+1
	 end
	 self:center()
	 for i,data in pairs(self.mapData.data) do
	    local char = data.char
	    local l = letter.newLetter(self, char, data.start.x, data.start.y)
	    local l = letter.newGoal(self, char, data.goal.x, data.goal.y)

	 end

      else
	 local y = 0
	 local d = self.cellD
	 while y < self.cellH do
	    local x = 0
	    while x < self.cellW do
	       local cell = Map.newCell{x=x, y=y, d=d}
	       self:insert(cell)
	       x = x + 1
	    end
	    y = y + 1
	 end
	 
      end
   end

   function map:clear()
      while self.numChildren > 1 do
	 local cell = self:remove(self.numChildren)
	 cell = nil
      end
   end
   
   function map:touch(event)
      self.x = event.x
      self.y = event.y
   end

   function map:activate()
      Runtime:addEventListener("touch", self)
   end

   function map:deactivate()
      Runtime:removeEventListener("touch", self)
   end

   function map:move(x,y)
      self.x = self.x + x
      self.y = self.y + y
   end
   
   function map:center(recenter)
      local player = self.player
      if recenter then
	 local x, y = player:localToContent(0,0)	 
	 transition.to(player, {x = self.mapData.start.x*50, 
				y=self.mapData.start.y * 50, 
				onComplete=function(event) 
				   player:invincible(event)
				end})
	 transition.to(self, {x = display.contentWidth*0.5 - self.mapData.start.x * 50,
			      y = display.contentHeight*0.3 - self.mapData.start.y * 50})
      else
	 player.x = self.mapData.start.x * 50
	 player.y = self.mapData.start.y * 50
	 local x, y = player:localToContent(0,0)	 
      
	 self.x = display.contentWidth*0.5 - x 
	 self.y = display.contentHeight*0.3 - y 
      end
      
   end
   return map

end
return Map